

. .\Lib.Strings.ps1
. .\Lib.MimeType.ps1
. .\Lib.WebPage.ps1
. .\Lib.Imaging.ps1


#====================================================================
#
#		IMAGE BROWSER WEB APPLICATION
#
#====================================================================


#--------------------------------------------------------------------
Function Get-HtmlFolderList
{
	param
	(
		  [psobject] $WebRequest
	)
	[string] $html = ""
	$html += "<div id=""folder_list"">"
	$html += "<b>Folders</b>"
	$html += "<table>"
	[string] $path = $WebRequest.WebServer.ApplicationFolder
	[string] $url = $WebRequest.Protocol + "://" + $WebRequest.WebServer.ServerAddress
	if( $WebRequest.ResourceName -ne "" )
	{
		$path += "\" + $WebRequest.ResourceName
		$url += "/" + $WebRequest.ResourceName.Replace( "\", "/" )
	}
	[psobject] $Items = Get-ChildItem -Path $path
	[psobject] $Item = $null
	$html += "<tr>"
	[int] $CellCount = 0
	if( $Items -ne $null )
	{
		$Items = $Items | Sort-Object -Property Name
		foreach( $Item in $Items )
		{
			[string] $Attrs = $Item.Attributes.ToString()
			if( $Attrs.Contains( "Directory" ) )
			{
				$html += "<td>[ <a href=""$( $url + "/" + $Item.Name )"">$( $Item.Name )</a> ]</td>"
				$CellCount += 1
				if( ($CellCount % 5) -eq 0 )
					{ $html += "</tr><tr>" }
			}
		}
	}
	$html += "</tr>"
	$html += "</table>"
	$html += "</div>"
	return $html
}


#--------------------------------------------------------------------
Function Get-HtmlFileList
{
	param
	(
		  [psobject] $WebRequest
	)
	[string] $html = ""
@"
	<div id="file_list">
	<b>Files</b>
	<table>
		<tr>
"@
	[string] $path = $WebRequest.WebServer.ApplicationFolder
	[string] $url = $WebRequest.Protocol + "://" + $WebRequest.WebServer.ServerAddress
	if( $WebRequest.ResourceName -ne "" )
	{
		$path += "/" + $WebRequest.ResourceName
		$url += "/" + $WebRequest.ResourceName.Replace( "\", "/" )
	}
	[psobject] $Items = Get-ChildItem -Path $path
	[psobject] $Item = $null
	[int] $CellCount = 0
	[string] $MimeType = ""
	[string] $ItemUrl = ""
	[string] $ItemName = ""
	if( $Items -ne $null )
	{
		$Items = $Items | Sort-Object -Property Name
		foreach( $Item in $Items )
		{
			[string] $Attrs = $Item.Attributes.ToString()
			if( -not $Attrs.Contains( "Directory" ) )
			{
				$MimeType = Get-ItemMimeType $Item.Name
				if( $MimeType -like "image/*" )
				{
					$ItemUrl = $url + "/" + $Item.Name
					$ItemName = $Item.Name
@"
					<td>
						<a href="$ItemUrl">
							<img src="$( $ItemUrl )?mime=image&width=100&height=100" alt="$ItemName" /><br />$ItemName
						</a>
					</td>
"@
					$CellCount += 1
					if( ($CellCount % 5) -eq 0 )
						{ "</tr><tr>" }
				}
			}
		}
	}
@"
		</tr>
	</table>
	</div>
"@
	return $html
}


#--------------------------------------------------------------------
Function Process-Web-GET
{
	param
	(
		  [psobject] $WebRequest
	)

	trap
	{
		Write-Error "`nError - $_"
		return $false
	}

	[psobject] $WebResponse = New-WebResponse
	$WebResponse.WebRequest		= $WebRequest
	$WebResponse.Resource		= $null
	$WebResponse.ResponseCode	= "501 Not Implemented"
	
	# Construct the request path.
	[string] $ResourcePath = $WebRequest.WebServer.ApplicationFolder
	[string] $ResourceName = $WebRequest.ResourceName

	# Check if requested path is a directory.
	if( Test-Path -Path $( $ResourcePath + "\" + $ResourceName ) -PathType container )
	{
		# Build the web page.
		[string] $html = Get-HtmlHeader $WebRequest -Title "PowerShell WebApp: Image Browser"
		$html += "<h3>Image Browser</h3>"
		$html += Get-HtmlNavigationMenu $WebRequest
		$html += "<br />"
		$html += Get-HtmlFolderList $WebRequest
		$html += "<br />"
		$html += Get-HtmlFileList $WebRequest
		$html += "<br />"
		$html += Get-HtmlFooter $WebRequest
		
		# Package the html in a new resource..
		[psobject] $Resource = New-Resource
		$Resource.Root = $ResourcePath
		$Resource.Path = $ResourceName
		$Resource.Name = ""
		$Resource.Mime = "text/html"
		$Resource.Data = AsciiString2Bytes( $html )
		$Resource.Size = $Resource.Data.Length

		# Construct the response.
		$WebResponse.Resource = $Resource
		$WebResponse.ResponseCode = "200 OK"
	}
	elseif( Test-Path -Path $( $ResourcePath + "\" + $ResourceName ) -PathType leaf )
	{
		[string] $mime = ""
		if( $WebRequest.Arguments.ContainsKey( "mime" ) )
			{ $mime = $WebRequest.Arguments.Item( "mime" ) }
		
		[psobject] $Resource = $null
		if( $mime -eq "image" )
		{
			$mime = Get-ItemMimeType $( $ResourcePath + "\" + $ResourceName )
			if( $mime -like "image/*" )
			{
				# Load as an image.
				[System.Drawing.Image] $Image = Get-Image $( $ResourcePath + "\" + $ResourceName )
				if( $Image -ne $null )
				{
					[int] $Width = 0
					[int] $Height = 0
					if( $WebRequest.Arguments.ContainsKey( "width" ) )
						{ $Width = $WebRequest.Arguments.Item( "width" ) }
					if( $WebRequest.Arguments.ContainsKey( "height" ) )
						{ $Height = $WebRequest.Arguments.Item( "height" ) }
#Write-Host "Image ( $($Image.Width) x $($Image.Height) ) -> ( $Width x $Height )."
					if( ($Width -ne 0) -or ($Height -ne 0) )
						{ $Image = Size-Image $Image $Width $Height }
#Write-Host "Image ( $($Image.Width) x $($Image.Height) )."

					# Package a new resource.
					$Resource = New-Resource
					$Resource.Root = $ResourcePath
					$Resource.Path = $ResourceName
					$Resource.Name = ""
					$Resource.Mime = $mime
					$Resource.Data = Serialize-Image $Image
					$Resource.Size = $Resource.Data.Length
				}
			}
		}

		if( $Resource -eq $null )
		{
			# Load as a binary file.
			$Resource = Read-Resource $ResourcePath $ResourceName
		}
		
		# Set the resource and response code.
		if( $Resource -eq $null )
		{
			$WebResponse.ResponseCode = "404 Not Found"
		}
		else
		{
			$WebResponse.Resource = $Resource
			$WebResponse.ResponseCode = "200 OK"
		}
	}
	
	# Send the response.
	[bool] $b = Send-WebResponse $WebResponse $true
	if( $b -eq $false ) { return $false }

	# Return, OK
	return $true
}

